package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.mvp.contract.OrderDetailContract;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;

import java.util.List;

import io.reactivex.Observable;

/**
 * @author fgs
 * @date 2020/4/1
 * @description
 **/
public class OrderDetailPresenter extends OrderDetailContract.Presenter {
    private Context mContext;
    private OrderModel mModel;
    private IOrderStatus mIOrderStatus;

    public OrderDetailPresenter(Context context) {
        mContext = context;
        mModel = new OrderModel();
    }

    public void setOrderStatus(IOrderStatus iOrderStatus) {
        mIOrderStatus = iOrderStatus;
    }


    @Override
    public void requestOrderDetail(Observable observable, boolean isDialog, int orderType) {
        if (orderType == OrderType.TYPE_FREIGHT) {
            mModel.orderDetailModel(mContext, observable, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderInfo>>() {
                @Override
                public void onNext(CommonResponse<OrderInfo> commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        OrderInfo info = commonResponse.getData();
                        if (null != info && mIOrderStatus != null) {
                            getView().showOrderDetailList(mIOrderStatus.getOrderDetailList(info));
                        }
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    getView().setMsg(e.message);
                }
            });
        } else {
            mModel.orderListModel(mContext, observable, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo<OrderInfo>>>() {
                @Override
                public void onNext(CommonResponse<CommonDataListInfo<OrderInfo>> commonResponse) {
                    if (null != getView()) {
                        if (commonResponse.getCode() == 20000) {
                            CommonDataListInfo data = commonResponse.getData();
                            List<OrderInfo> list = data.records;
                            if (list != null && list.size() > 0) {
                                OrderInfo info = list.get(0);
                                if (null != info && mIOrderStatus != null) {
                                    getView().setOrderInfo(info);
                                    getView().showOrderDetailList(mIOrderStatus.getOrderDetailList(info));
                                }
                            } else {
                                getView().showEmptyView();
                            }
                        } else {
                            getView().showEmptyView();
                            getView().setMsg(commonResponse.getMsg());
                        }
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    if (null != getView()) {
                        getView().setMsg(e.message);
                        getView().showEmptyView();
                    }
                }
            });
        }


    }
}
